package com.yuexunit.finance.member.action.v1;


import java.util.ArrayList;
import java.util.List;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.member.action.v1.model.MonthEarningDetailInfo;
import com.yuexunit.finance.member.entity.MonthEarningDetailEntity;
import com.yuexunit.finance.member.service.MonthEarningDetailService;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementEntity;
import com.yuexunit.finance.project.service.FinanceContractService;
import com.yuexunit.finance.project.service.FinanceMoneyStatementService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;

@Implements(MonthEarningDetailApi.class)
public class MonthEarningDetailApiImpl implements MonthEarningDetailApi {
	
	private MonthEarningDetailService monthEarningDetailService = ComponentFactory.loadClass(MonthEarningDetailService.class);

	private FinanceContractService financeContractService = ComponentFactory.loadClass(FinanceContractService.class);
	
	private FinanceMoneyStatementService financeMoneyStatementService = ComponentFactory.loadClass(FinanceMoneyStatementService.class);
	
	@Override
	public PageList<MonthEarningDetailInfo> inquiryMonthEarningDetailList(
			PageCriteria page, Integer year, Integer month,
			String financeNumber, Integer productType, Long enterpriseId) {
		PageList<MonthEarningDetailEntity> monthEarningDetailList = monthEarningDetailService.inquiryMonthEarningDetailList(page, year, month, financeNumber, productType, enterpriseId);
		PageList<MonthEarningDetailInfo> monthEarningDetailInfoList = new PageList<>(page, monthEarningDetailList.getTotalRecordCount());
		for (MonthEarningDetailEntity monthEarningDetailEntity : monthEarningDetailList) {
			MonthEarningDetailInfo monthEarningDetailInfo = new MonthEarningDetailInfo();
			BeanUtils.copyProperties(monthEarningDetailEntity, monthEarningDetailInfo);
			
			monthEarningDetailInfo.setProductType(monthEarningDetailEntity.getProductType().ordinal());
			monthEarningDetailInfo.setDays(DateUtils.daysBetween(monthEarningDetailEntity.getSettlementStart(), monthEarningDetailEntity.getSettlementEnd()));
			
			FinanceMoneyStatementEntity fmse = financeMoneyStatementService.inquireFinanceMoneyStatementBySourceId(monthEarningDetailEntity.getMonthEarningDetailId());
			if (fmse != null) {
				monthEarningDetailInfo.setMoneyStatementId(fmse.getMoneyStatementId());
				monthEarningDetailInfo.setStatementNumber(fmse.getStatementNumber());
				monthEarningDetailInfo.setAmount(fmse.getAmount());
				monthEarningDetailInfo.setReceiptAmount(fmse.getReceiptAmount());
			}
			
			monthEarningDetailInfoList.add(monthEarningDetailInfo);
		}
		
		return monthEarningDetailInfoList;
	}
	
	@Override
	public List<MonthEarningDetailInfo> inquiryMonthEarningDetailAllList(Integer year, Integer month,
			String financeNumber, Integer productType, Long enterpriseId) {
		List<MonthEarningDetailEntity> monthEarningDetailList = monthEarningDetailService.inquiryMonthEarningDetailList(year, month, financeNumber, productType, enterpriseId);
		List<MonthEarningDetailInfo> monthEarningDetailInfoList = new ArrayList<>();
		
		for (MonthEarningDetailEntity monthEarningDetailEntity : monthEarningDetailList) {
			MonthEarningDetailInfo monthEarningDetailInfo = new MonthEarningDetailInfo();
			BeanUtils.copyProperties(monthEarningDetailEntity, monthEarningDetailInfo);
			
			monthEarningDetailInfo.setProductType(monthEarningDetailEntity.getProductType().ordinal());
			monthEarningDetailInfo.setProductTypeView(monthEarningDetailEntity.getProductType().getValue());
			/*monthEarningDetailInfo.setFinanceState(monthEarningDetailEntity.getFinanceState().ordinal());*/
			FinanceContractEntity fce = financeContractService.inquireFinanceIdContract(monthEarningDetailEntity.getFinanceId(), 2);
			monthEarningDetailInfo.setFinanceContractSerialNumber(fce.getFinanceContractSerialNumber());
			monthEarningDetailInfo.setDays(DateUtils.daysBetween(monthEarningDetailEntity.getSettlementStart(), monthEarningDetailEntity.getSettlementEnd()));
			
			monthEarningDetailInfoList.add(monthEarningDetailInfo);
		}
		
		return monthEarningDetailInfoList;
	} 

	@Override
	public MonthEarningDetailInfo inquiryMonthEarningDetailFinance(
			Long monthEarningDetailId) {
		MonthEarningDetailEntity monthEarningDetailEntity = monthEarningDetailService.inquiryMonthEarningDetailFinance(monthEarningDetailId);
		if(monthEarningDetailEntity == null) {
			return null;
		}
		MonthEarningDetailInfo monthEarningDetailInfo = new MonthEarningDetailInfo();
		BeanUtils.copyProperties(monthEarningDetailEntity, monthEarningDetailInfo);
		monthEarningDetailInfo.setProductType(monthEarningDetailEntity.getProductType().ordinal());
		
		return monthEarningDetailInfo;
	}
}
